package Z.dailyExercise.April;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class _102二叉树的层序遍历 {
    public List<List<Integer>> levelOrder(TreeNode root) {

        if (root == null){
            return new ArrayList<>();
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        List<List<Integer>> res = new ArrayList<>();
        while (queue.size()>0){
            List<Integer> list = new ArrayList<>();
            int amount = queue.size();
            while (amount >0){
                TreeNode node = queue.poll();
                amount--;
                list.add(node.val);
                if (node.left!= null){
                    queue.add(node.left);
                }
                if (node.right != null){
                    queue.add(node.right);
                }

            }
            res.add(list);
        }
        return res;

    }


}
